package com.ruoyi.system.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import ch.qos.logback.core.joran.spi.XMLUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.FxyFinancialSocialsecurityMapper;
import com.ruoyi.system.domain.FxyFinancialSocialsecurity;
import com.ruoyi.system.service.IFxyFinancialSocialsecurityService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 五险一金设置表Service业务层处理
 * 
 * @author XU
 * @date 2025-02-25
 */
@Service
@Transactional
public class FxyFinancialSocialsecurityServiceImpl implements IFxyFinancialSocialsecurityService {
    @Autowired
    private FxyFinancialSocialsecurityMapper fxyFinancialSocialsecurityMapper;

    /**
     * 查询五险一金设置表
     *
     * @param id 五险一金设置表主键
     * @return 五险一金设置表
     */
    @Override
    public FxyFinancialSocialsecurity selectFxyFinancialSocialsecurityById(Long id) {
        return fxyFinancialSocialsecurityMapper.selectFxyFinancialSocialsecurityById(id);
    }

    /**
     * 查询五险一金设置表列表
     *
     * @param fxyFinancialSocialsecurity 五险一金设置表
     * @return 五险一金设置表
     */
    @Override
    public List<FxyFinancialSocialsecurity> selectFxyFinancialSocialsecurityList(FxyFinancialSocialsecurity fxyFinancialSocialsecurity) {
        return fxyFinancialSocialsecurityMapper.selectFxyFinancialSocialsecurityList(fxyFinancialSocialsecurity);
    }

    /**
     * 新增五险一金设置表
     *
     * @param fxyFinancialSocialsecurity 五险一金设置表
     * @return 结果
     */
    @Override
    public int insertFxyFinancialSocialsecurity(FxyFinancialSocialsecurity fxyFinancialSocialsecurity) {
        //  添加时确保缴纳项目的唯一
//        查询数据库中是否存在相同数据
        boolean exists = fxyFinancialSocialsecurityMapper.checkExists(fxyFinancialSocialsecurity);
        if (exists) {
            // 如果存在相同的缴纳项目，抛出异常或返回错误码
            throw new IllegalArgumentException("亲，保存失败。缴纳项目名称重复");
        }
        return fxyFinancialSocialsecurityMapper.insertFxyFinancialSocialsecurity(fxyFinancialSocialsecurity);
    }

    /**
     * 修改五险一金设置表
     *
     * @param fxyFinancialSocialsecurity 五险一金设置表
     * @return 结果
     */
    @Override
    public int updateFxyFinancialSocialsecurity(FxyFinancialSocialsecurity fxyFinancialSocialsecurity) {
        //       当缴纳方式切换为按金额缴纳时 清空个人缴纳比例和 公司缴纳比例的 值
        if(fxyFinancialSocialsecurity.getBaseNum()==2){
            fxyFinancialSocialsecurity.setPersonPortion("--");
            fxyFinancialSocialsecurity.setCompanyPortion("--");
        }else {
            fxyFinancialSocialsecurity.setPersonPay("--");
            fxyFinancialSocialsecurity.setCompanyPay("--");
        }
        return fxyFinancialSocialsecurityMapper.updateFxyFinancialSocialsecurity(fxyFinancialSocialsecurity);
    }

    /**
     * 批量删除五险一金设置表
     *
     * @param ids 需要删除的五险一金设置表主键
     * @return 结果
     */
    @Override
    public int deleteFxyFinancialSocialsecurityByIds(Long[] ids) {
        return fxyFinancialSocialsecurityMapper.deleteFxyFinancialSocialsecurityByIds(ids);
    }

    @Override
    public String importSocialsecurity(List<FxyFinancialSocialsecurity> socialsecurityList) {
        if (socialsecurityList == null || socialsecurityList.isEmpty()) {
            throw new ServiceException("导入数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (FxyFinancialSocialsecurity socialsecurity : socialsecurityList) {
            try {
                // 验证必填项
                validateSocialsecurity(socialsecurity);

                // 插入数据
                fxyFinancialSocialsecurityMapper.insertFxyFinancialSocialsecurity(socialsecurity);
                successNum++;
                successMsg.append("<br/>" + successNum + "、项目 " + socialsecurity.getProject() + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、项目 " + socialsecurity.getProject() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }

        return successMsg.toString();
        }


    private void validateSocialsecurity(FxyFinancialSocialsecurity socialsecurity) {
        if (socialsecurity.getProject() == null || socialsecurity.getProject().trim().isEmpty()) {
            throw new ServiceException("缴纳项目不能为空");
        }
//        缴纳项目不能重复
        if (fxyFinancialSocialsecurityMapper.checkExists(socialsecurity)) {
            throw new ServiceException("项目名重复");
        }

        if (socialsecurity.getBaseNum() == null) {
            throw new ServiceException("缴纳方式不能为空");
        }

        // 按比例缴纳
        if ("1".equals(socialsecurity.getBaseNum())) {
            if (socialsecurity.getCompanyPortion() == null) {
                throw new ServiceException("按比例缴纳时，公司缴纳比例不能为空");
            }
            if (socialsecurity.getPersonPortion() == null) {
                throw new ServiceException("按比例缴纳时，个人缴纳比例不能为空");
            }
        }
        // 按金额缴纳
        else if ("2".equals(socialsecurity.getBaseNum())) {
            if (socialsecurity.getCompanyPay() == null) {
                throw new ServiceException("按金额缴纳时，公司缴纳金额不能为空");
            }
            if (socialsecurity.getPersonPay() == null) {
                throw new ServiceException("按金额缴纳时，个人缴纳金额不能为空");
            }
        }
    }

}






